/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.dejax.service.impl;

import com.dejax.domain.DevopsWorkorder;
import com.dejax.utils.ValidationUtil;
import com.dejax.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.dejax.repository.DevopsWorkorderRepository;
import com.dejax.service.DevopsWorkorderService;
import com.dejax.service.dto.DevopsWorkorderDto;
import com.dejax.service.dto.DevopsWorkorderQueryCriteria;
import com.dejax.service.mapstruct.DevopsWorkorderMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.dejax.utils.PageUtil;
import com.dejax.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Richer
* @date 2022-01-07
**/
@Service
@RequiredArgsConstructor
public class DevopsWorkorderServiceImpl implements DevopsWorkorderService {

    private final DevopsWorkorderRepository devopsWorkorderRepository;
    private final DevopsWorkorderMapper devopsWorkorderMapper;

    @Override
    public Map<String,Object> queryAll(DevopsWorkorderQueryCriteria criteria, Pageable pageable){
        criteria.setFlag(0);
        Page<DevopsWorkorder> page = devopsWorkorderRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(devopsWorkorderMapper::toDto));
    }

    @Override
    public List<DevopsWorkorderDto> queryAll(DevopsWorkorderQueryCriteria criteria){
        criteria.setFlag(0);
        return devopsWorkorderMapper.toDto(devopsWorkorderRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DevopsWorkorderDto findById(String workorderid) {
        DevopsWorkorder devopsWorkorder = devopsWorkorderRepository.findById(workorderid).orElseGet(DevopsWorkorder::new);
        ValidationUtil.isNull(devopsWorkorder.getWorkorderid(),"DevopsWorkorder","workorderid",workorderid);
        return devopsWorkorderMapper.toDto(devopsWorkorder);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DevopsWorkorderDto create(DevopsWorkorder resources) {
        resources.setWorkorderid(IdUtil.simpleUUID());
        resources.setFlag(0);
        return devopsWorkorderMapper.toDto(devopsWorkorderRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DevopsWorkorder resources) {
        DevopsWorkorder devopsWorkorder = devopsWorkorderRepository.findById(resources.getWorkorderid()).orElseGet(DevopsWorkorder::new);
        ValidationUtil.isNull( devopsWorkorder.getWorkorderid(),"DevopsWorkorder","id",resources.getWorkorderid());
        devopsWorkorder.copy(resources);
        devopsWorkorderRepository.save(devopsWorkorder);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String workorderid : ids) {
//            devopsWorkorderRepository.deleteById(workorderid);
        }
    }

    @Override
    public void download(List<DevopsWorkorderDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DevopsWorkorderDto devopsWorkorder : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("工单说明", devopsWorkorder.getOrdersdesc());
            map.put("工单状态", devopsWorkorder.getOrderstatus());
            map.put("执行结果状态", devopsWorkorder.getResultstatus());
            map.put("指定审核人", devopsWorkorder.getAssignuser());
            map.put("定时执行", devopsWorkorder.getExectime());
            map.put(" flag",  devopsWorkorder.getFlag());
            map.put(" notes",  devopsWorkorder.getNotes());
            map.put(" reguser",  devopsWorkorder.getReguser());
            map.put(" regtime",  devopsWorkorder.getRegtime());
            map.put(" moduser",  devopsWorkorder.getModuser());
            map.put(" modtime",  devopsWorkorder.getModtime());
            map.put("ip", devopsWorkorder.getJenkinsid());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}